﻿

CREATE PROCEDURE [dbo].[balcon_GenNCExtrasCont]
	-- Add the parameters for the stored procedure here
	@cont TipCont,
	@CodUnitate smallint,
	@DeLaData datetime,
	@PanaLaData datetime,
	@An smallint,
	@strFiltruCapitol varchar(2000),
	@strFiltruNrNota varchar(2000)
AS
BEGIN
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT b.NumarDoc, b.DataDoc, b.NumarNota, a.ContDebit, a.ContCredit, 
    b.IDJurnal, a.ValoareLei, a.DataDocJust, a.TipDocJust, a.NumarDocJust, a.ExplicatiiDocJust, 
    b.TipFormula, c.Categorie, 'D' AS Tip, a.CodUnitate
	FROM  [Rulaje] a 
	INNER JOIN  [RulajeDoc] b ON a.IDDocument = b.IDDocument 
	INNER JOIN  [Conturi] c ON a.ContDebit = c.Cont AND c.CodUnitate = a.CodUnitate
    WHERE [ContDebit]<>'N/A' AND a.CodUnitate = @CodUnitate 
         AND (LEN(@cont)=0 OR a.ContDebit like  @cont + '%')
         AND b.DataDoc >= @DeLaData AND b.DataDoc<= @PanaLaData
         AND (LEN(LTRIM(RTRIM(@strFiltruNrNota)))=0 OR (b.NumarNota NOT IN (SELECT fart.value FROM dbo.SplitParameters(@strFiltruNrNota,',') fart)))
         AND (LEN(LTRIM(RTRIM(@strFiltruCapitol)))=0 OR (b.IDJurnal IN (SELECT fart.value FROM dbo.SplitParameters(@strFiltruCapitol,',') fart)))

    UNION 
    SELECT b.NumarDoc, b.DataDoc, b.NumarNota, a.[ContCredit] AS [ContDebit], a.[ContDebit] AS ContCredit, 
    b.IDJurnal, a.ValoareLei, a.DataDocJust, a.TipDocJust, a.NumarDocJust, a.ExplicatiiDocJust, 
    b.TipFormula, c.Categorie, 'C' AS Tip, a.CodUnitate
	FROM  [Rulaje] a 
	INNER JOIN  [RulajeDoc] b ON a.IDDocument = b.IDDocument 
	INNER JOIN  [Conturi] c ON a.ContDebit = c.Cont AND c.CodUnitate = a.CodUnitate
	WHERE [ContCredit]<>'N/A' AND a.CodUnitate = @CodUnitate
		AND (LEN(@cont)=0 OR a.ContCredit like  @cont + '%')
         AND b.DataDoc >= @DeLaData AND b.DataDoc<= @PanaLaData
         AND (LEN(LTRIM(RTRIM(@strFiltruNrNota)))=0 OR (b.NumarNota NOT IN (SELECT fart.value FROM dbo.SplitParameters(@strFiltruNrNota,',') fart)))
         AND (LEN(LTRIM(RTRIM(@strFiltruCapitol)))=0 OR (b.IDJurnal IN (SELECT fart.value FROM dbo.SplitParameters(@strFiltruCapitol,',') fart)))
    ORDER BY b.DataDoc

END